package com.example.demo.niuke.huawei_b;

import com.vertica.dsi.dataengine.interfaces.IArray;

import java.util.*;

/**
 * ******************************************************
 *
 * @author liugh9
 * @version 1.0
 * @classname _11数据分类处理
 * @description
 * @date 2023/09/02 8:46
 * <p>
 * ******************************************************
 */
public class _11数据分类处理 {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 数据读取
        String[] IArray = new String[in.nextInt()];
        int i = 0;
        while (i < IArray.length) {
            IArray[i++] = in.next();
        }
        // 数据读取及其排序去重
        List<Integer> RList = new LinkedList<>();
        int rn = in.nextInt();
        int r = 0;
        while (r++ < rn) {
            int next = in.nextInt();
            if (!RList.contains(next)) {
                RList.add(next);
            }
        }
        Collections.sort(RList);
        System.out.println(RList);
        LinkedList<String> res = new LinkedList<>();
        for (Integer item : RList) {
            TreeMap<Integer, String> itemResult = patternI(IArray, item);
            if (!itemResult.isEmpty()) {
                res.add(String.valueOf(item));
                res.add(String.valueOf(itemResult.size()));
                for (Integer key : itemResult.keySet()) {
                    String value = itemResult.get(key);
                    res.add(String.valueOf(key));
                    res.add(value);
                }
            }
        }
        res.addFirst(String.valueOf(res.size()));
        System.out.println(String.join(" ", res));

    }

    private static TreeMap<Integer, String> patternI(String[] iArray, Integer item) {
        TreeMap<Integer, String> res = new TreeMap<>(Integer::compareTo);
        for (int i = 0; i < iArray.length; i++) {
            if (iArray[i].contains(String.valueOf(item))) {
                res.put(i, iArray[i]);
            }
        }

        return res;
    }

}
